package com.kou.filter;

import com.kou.pojo.Goods;
import com.kou.pojo.Type;
import com.kou.service.GoodsService;
import com.kou.service.GoodsTypeService;
import com.kou.service.impl.GoodsServiceImpl;
import com.kou.service.impl.GoodsTypeServiceImpl;
import com.kou.utils.StrUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author SteinY
 * @create 2022-03-25 10:18
 * @description:
 */
@WebFilter({"/admin/showGoods.jsp","/admin/addGoods.jsp"})
public class GoodsFilter implements Filter {
    GoodsService goodsService = new GoodsServiceImpl();
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    //从cookie中获取两周之内的用户信息
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        List<Goods> goods = goodsService.findAll();
        for (Goods good : goods) {
            GoodsTypeService goodsTypeService = new GoodsTypeServiceImpl();
            String parentName = goodsTypeService.findParentName(good.getTid());
            if (!StrUtils.empty(parentName)){
                parentName = "无";
            }
            good.setTypeName(parentName);
        }

       // System.out.println(types.size());
        request.getSession().setAttribute("goodsList",goods);
        //放行！！
        filterChain.doFilter(request,response);
    }

    @Override
    public void destroy() {

    }
}